<template>
  <div class="demand-edit">
    <el-dialog v-bind="$attrs" :title="state.dialogTitle" v-model="state.modalVisible" :before-close="cancelDialog"
      :append-to-body="true" :destroy-on-close="true" :close-on-click-modal="false" :close-on-press-escape="false"
      :lock-scroll="false" class="demand-dialog" draggable>
      <el-form ref="demandRef" :model="state.form" :rules="state.rules" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="处理单名称" prop="demandname">
              <el-input v-model="state.form.demandname" placeholder="处理单名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理状态" prop="demandstatus">
              <el-input v-model="state.form.demandstatus" placeholder="处理状态" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理结果" prop="demandovercontent">
              <el-input v-model="state.form.demandovercontent" placeholder="处理结果" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import useDemandManageManageStore from '@store/modules/demandManage'
import { filterObject } from '@/utils/index'
import { nextTick } from 'vue';

const demandManageStore = useDemandManageManageStore()
const { proxy } = getCurrentInstance()
const demandRef = ref(null)

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  form: {},
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  },
})

const emits = defineEmits(['refresh'])

const getDataView = (demandid) => {
  nextTick(async () => {
    const result = await demandManageStore.view({ "demandid": demandid })
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

const submitDialog = () => {
  demandRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.demandid = state.editView.demandid
        await demandManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await demandManageStore.add(state.form)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      demandRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

const cancelDialog = () => {
  state.editView = {}
  demandRef.value.resetFields()
  state.modalVisible = false
}

const openDialog = async (data = {}) => {
  if (Object.keys(data).length) await getDataView(data.demandid)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.demandid}】` : '新增'
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
